package com.wande.dataplatform.filecollection.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.filecollection.domain.bo.UnstructuredFileBo;
import com.wande.dataplatform.filecollection.domain.vo.UnstructuredFileVo;
import com.wande.dataplatform.filecollection.service.IUnstructuredFileService;
import com.wande.dataplatform.filecollection.security.FileSecurityValidator;
import com.wande.dataplatform.filecollection.security.AuditLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 非结构化文件控制器
 *
 * @author wande
 */
@Validated
@RestController
@RequestMapping("/dataplatform/file-collection/unstructured")
@RequiredArgsConstructor
public class UnstructuredFileController extends BaseController {

    private final IUnstructuredFileService unstructuredFileService;
    private final FileSecurityValidator securityValidator;
    private final AuditLogService auditLogService;

    /**
     * 上传单个文件
     *
     * @param file 文件
     * @param bo   业务对象
     * @return 文件ID
     */
    @SaCheckPermission("dataplatform:file-collection:unstructured:upload")
    @Log(title = "非结构化文件", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public R<Long> uploadFile(@RequestParam("file") MultipartFile file,
                              @ModelAttribute UnstructuredFileBo bo) {
        // 安全验证
        securityValidator.validateMultipartFile(file);
        
        Long fileId = unstructuredFileService.uploadFile(file, bo);
        
        // 记录审计日志
        auditLogService.logFileUpload(file.getOriginalFilename(), file.getSize(), 
            bo.getFileType());
        
        return R.ok(fileId);
    }

    /**
     * 批量上传文件
     *
     * @param files 文件列表
     * @param bo    业务对象
     * @return 文件ID列表
     */
    @SaCheckPermission("dataplatform:file-collection:unstructured:upload")
    @Log(title = "非结构化文件", businessType = BusinessType.INSERT)
    @PostMapping("/upload/batch")
    public R<List<Long>> uploadFiles(@RequestParam("files") List<MultipartFile> files,
                                     @ModelAttribute UnstructuredFileBo bo) {
        // 安全验证每个文件
        for (MultipartFile file : files) {
            securityValidator.validateMultipartFile(file);
        }
        
        List<Long> fileIds = unstructuredFileService.uploadFiles(files, bo);
        
        // 记录审计日志
        for (MultipartFile file : files) {
            auditLogService.logFileUpload(file.getOriginalFilename(), file.getSize(), 
                bo.getFileType());
        }
        
        return R.ok(fileIds);
    }

    /**
     * 从指定来源采集文件
     *
     * @param sourceType   来源类型
     * @param sourceConfig 来源配置
     * @return 成功消息
     */
    @SaCheckPermission("dataplatform:file-collection:unstructured:collect")
    @Log(title = "非结构化文件", businessType = BusinessType.OTHER)
    @PostMapping("/collect")
    public R<Void> collectFiles(@RequestParam String sourceType,
                                @RequestParam String sourceConfig) {
        unstructuredFileService.collectFiles(sourceType, sourceConfig);
        return R.ok();
    }

    /**
     * 查询文件详情
     *
     * @param id 文件ID
     * @return 文件详情
     */
    @SaCheckPermission("dataplatform:file-collection:unstructured:query")
    @GetMapping("/{id}")
    public R<UnstructuredFileVo> getFile(@PathVariable Long id) {
        UnstructuredFileVo vo = unstructuredFileService.getFile(id);
        return R.ok(vo);
    }

    /**
     * 分页查询文件列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 文件列表
     */
    @SaCheckPermission("dataplatform:file-collection:unstructured:list")
    @GetMapping("/list")
    public TableDataInfo<UnstructuredFileVo> listFiles(UnstructuredFileBo bo, PageQuery pageQuery) {
        return unstructuredFileService.listFiles(bo, pageQuery);
    }

    /**
     * 删除文件
     *
     * @param id 文件ID
     * @return 成功消息
     */
    @SaCheckPermission("dataplatform:file-collection:unstructured:remove")
    @Log(title = "非结构化文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> deleteFile(@PathVariable Long id) {
        unstructuredFileService.deleteFile(id);
        return R.ok();
    }

    /**
     * 批量删除文件
     *
     * @param ids 文件ID列表
     * @return 成功消息
     */
    @SaCheckPermission("dataplatform:file-collection:unstructured:remove")
    @Log(title = "非结构化文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/batch")
    public R<Void> deleteFiles(@RequestBody List<Long> ids) {
        unstructuredFileService.deleteFiles(ids);
        return R.ok();
    }

    /**
     * 获取文件下载URL
     *
     * @param id 文件ID
     * @return 下载URL
     */
    @SaCheckPermission("dataplatform:file-collection:unstructured:download")
    @GetMapping("/{id}/download")
    public R<String> getDownloadUrl(@PathVariable Long id) {
        String url = unstructuredFileService.getDownloadUrl(id);
        return R.ok(url);
    }

    /**
     * 提取文件元数据
     *
     * @param file 文件
     * @return 元数据
     */
    @SaCheckPermission("dataplatform:file-collection:unstructured:upload")
    @PostMapping("/metadata")
    public R<Map<String, Object>> extractMetadata(@RequestParam("file") MultipartFile file) {
        Map<String, Object> metadata = unstructuredFileService.extractMetadata(file);
        return R.ok(metadata);
    }
}
